using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class RVLoadingSprite : MonoBehaviour
{
	[SerializeField]
	public Sprite LoadingSprite;

	public Image DefaultImage;

	public Button RVButton;

	public bool HandleInteractivity;

	private Tween _currentTween;

	private Sprite _defaultSprite;

	private Color _defaultColor;

	private bool _suscribed;

	private void Awake()
	{
		if (RVButton == null)
		{
			RVButton = GetComponent<Button>();
		}
		if (DefaultImage == null)
		{
			DefaultImage = GetComponent<Image>();
		}
		if (RVButton != null)
		{
			RVButton.onClick.AddListener(PrepareCheckRV);
		}
	}

	private void OnEnable()
	{
		PrepareCheckRV();
	}

	private void PrepareCheckRV()
	{
		if (_currentTween != null && _currentTween.IsActive())
		{
			_currentTween.Kill();
		}
		if (DefaultImage != null)
		{
			if (_defaultSprite == null)
			{
				_defaultSprite = DefaultImage.sprite;
				_defaultColor = DefaultImage.color;
			}
			DefaultImage.transform.localRotation = Quaternion.identity;
		}
		if (HandleInteractivity && RVButton != null)
		{
			RVButton.interactable = false;
		}
		_currentTween = DOVirtual.DelayedCall(0.1f, DoCheckRV);
	}

	private void DoCheckRV()
	{
		OnRVReady(success: true);
	}

	private void OnDisable()
	{
		if (_currentTween != null && _currentTween.IsActive())
		{
			_currentTween.Kill();
		}
	}

	private void OnRVReady(bool success)
	{
		if (success)
		{
			_suscribed = false;
			if (HandleInteractivity && RVButton != null)
			{
				RVButton.interactable = true;
			}
			if (DefaultImage != null)
			{
				DefaultImage.sprite = _defaultSprite;
				DefaultImage.color = _defaultColor;
				DefaultImage.transform.localRotation = Quaternion.identity;
			}
			if (_currentTween != null && _currentTween.IsActive())
			{
				_currentTween.Kill();
			}
		}
	}
}
